package com.rongshu.view.login.presenter;

import android.content.Context;

import com.rongshu.api.FileUploadService;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.login.contract.SetPenNameView;
import com.rongshu.view.login.entity.PerfectPenName;
import com.rongshu.view.userInfo.entity.UpLoadImage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscriber;

public class SetPenNamePresenter extends BasePresenter<SetPenNameView> {

    public SetPenNamePresenter(Context context) {
        super(context);
    }

    /**
     * base64图片上传
     */
    public void postUploadFileBase64(String path) {
        //构建要上传的文件
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("AndroidFile", file.getName(), requestFile);
        FileUploadService.upload(body).subscribe(new Subscriber<UpLoadImage>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ((BaseActivity) mContext).showLoading(false);
            }

            @Override
            public void onNext(UpLoadImage upLoadImageResult) {
                if (upLoadImageResult.getErrCode() == 0) {
                    UpLoadImage.ResponseBean response = upLoadImageResult.getResponse();
                    if (response != null && response.getImgUrl() != null) {
                        String url = response.getImgUrl().get(0);
                        getView().uploadImgSuccess(url);
                    } else {
                        getView().iNetFailure(upLoadImageResult.getErrMsg());
                    }
                } else {
                    getView().iNetFailure(upLoadImageResult.getErrMsg());
                }
            }
        });
    }

    /**
     * 设置笔名
     */
    public void perfectPanName(PerfectPenName perfectPenName) {
        //获取保存的userId
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserInfo().getUser_id());
        RetorfitService.perfectPanName(UserInfoUtils.getUserInfo().getUser_id(),
                Common.encryptionSign(map), perfectPenName)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ((BaseActivity) mContext).showLoading(false);
                    }

                    @Override
                    public void onNext(INetBean penNameResponse) {
                        if (penNameResponse.getErrCode() == 0){
                            getView().onSetPenName();
                        } else {
                            getView().iNetFailure(penNameResponse.getErrMsg());
                        }
                    }
                });
    }
}
